﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using Housing;

public partial class HouseKeep_AdjustApply : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["noid"] == null || Request["longid"] == null)
        {
            WebHelper.AlertAndClose("链接错误，请重新操作！");
        }
    }
    protected void ddltype_SelectedIndexChanged(object sender, EventArgs e)
    {
        string noid = Request.QueryString["noid"].ToString();
        SubsidyRecord subsidy = new SubsidyRecord();
        //读取最新租金补贴数据
        subsidy.GetNewestSubsidyRecordByNoid(noid);
        if (subsidy.Adj_stoptime != null)
        {
            ViewState["substoptime"] = subsidy.Adj_stoptime;
            ckbsubsidy.Checked=true;
        }
        ReduceRecord reduce = new ReduceRecord();
        //读取最新租金减免数据
        reduce.GetNewestReduceRecordBynoid(noid);
        if (reduce.Adj_stoptime != null)
        {
            ViewState["reddate"] = reduce.Adj_stoptime;
            ViewState["redoldarea"] = string.IsNullOrEmpty(reduce.Adj_nowarea) ? "0" : reduce.Adj_nowarea;
            ViewState["redoldmoney"] = string.IsNullOrEmpty(reduce.Adj_nowmoney) ? "0" : reduce.Adj_nowmoney;
            ViewState["redoldreduce"] = string.IsNullOrEmpty(reduce.Adj_nowreduce) ? "0" : reduce.Adj_nowreduce;
            ViewState["redoldstoptime"] = string.IsNullOrEmpty(reduce.Adj_stoptime) ? "0" : reduce.Adj_stoptime;
            ckbreduce.Checked=true;
        }
        KindRecord kind = new KindRecord();
        //读取最新实物配租数据
        kind.GetNewestKindRecordBynoid(noid);
        if (kind.Adj_stoptime != null)
        {
            ViewState["kindstoptime"] = kind.Adj_stoptime;
            ViewState["kindoldarea"] = string.IsNullOrEmpty(kind.Adj_nowarea) ? "0" : kind.Adj_nowarea;
            ViewState["kindoldusearea"] = string.IsNullOrEmpty(kind.Adj_nowuserarea) ? "0" : kind.Adj_nowuserarea;
            ckbkind.Checked=true;
        }
        if (ddltype.SelectedValue == "增加、减少")
        {
            paneladd.Visible = true;
            panelcontinue.Visible = false;
            panelreduce.Visible = false;
            panelreissue.Visible = false;
            if (reduce.Adj_stoptime != null)
            {
                ckbred1.Checked = true;
                if (kind.Adj_stoptime != null)
                    ckbkind1.Checked = true;
                else
                    kind1.Visible = false;
            }
            else
            {
                sub1.Visible = false;
                if (kind.Adj_stoptime != null)
                {
                    ckbkind1.Checked = true;
                }
                else
                {
                    button.Visible = false;
                    lblmess.Text = "<br/>未找到需要调整的项";
                    kind1.Visible = false;
                }
            }
            txbname.Text = reduce.Adj_renter;
            txbaddress.Text = reduce.Adj_rentaddress;
            txbcheckarea.Text = reduce.Adj_nowarea;
            txbmonthmoney.Text=reduce.Adj_nowmoney;
            txbreducemoney.Text = reduce.Adj_nowreduce;
            txbkindaddress.Text = kind.Adj_location;
            txbkindarea.Text = kind.Adj_nowarea;
            txbkindusearea.Text = kind.Adj_nowuserarea;
        }
        else if (ddltype.SelectedValue == "续办")
        {
            BindDate();
            paneladd.Visible = false;
            panelcontinue.Visible = true;
            panelreduce.Visible = false;
            panelreissue.Visible = false;
            if (subsidy.Adj_stoptime != null)
            {
                if (subsidy.Adj_stoptime == "-1")
                    ddlyear1.SelectedValue = "无期";
                else
                {
                    ddlyear1.SelectedValue = subsidy.Adj_stoptime.Substring(0, 4);
                    if (subsidy.Adj_stoptime.Substring(4, 1) == "0")
                        ddlmonth1.SelectedValue = subsidy.Adj_stoptime.Substring(5, 1);
                    else
                        ddlmonth1.SelectedValue = subsidy.Adj_stoptime.Substring(4, 2);
                }
                ckbsubsidy.Enabled = true;
            }
            if (reduce.Adj_stoptime != null)
            {
                if (reduce.Adj_stoptime == "-1")
                    ddlyear2.SelectedValue = "无期";
                else
                {
                    ddlyear2.SelectedValue = reduce.Adj_stoptime.Substring(0, 4);
                    if (reduce.Adj_stoptime.Substring(4, 1) == "0")
                        ddlmonth2.SelectedValue = reduce.Adj_stoptime.Substring(5, 1);
                    else
                        ddlmonth2.SelectedValue = reduce.Adj_stoptime.Substring(4, 2);
                }
                ckbreduce.Enabled = true;
            }
            if (kind.Adj_stoptime != null)
            {
                if (kind.Adj_stoptime == "-1")
                    ddlyear2.SelectedValue = "无期";
                else
                {
                    ddlyear3.SelectedValue = kind.Adj_stoptime.Substring(0, 4);
                    if (kind.Adj_stoptime.Substring(4, 1) == "0")
                        ddlmonth3.SelectedValue = kind.Adj_stoptime.Substring(5, 1);
                    else
                        ddlmonth3.SelectedValue = kind.Adj_stoptime.Substring(4, 2);
                }
                ckbkind.Enabled = true;
            }
        }
        else if (ddltype.SelectedValue == "补发")
        {
            BindDate();
            paneladd.Visible = false;
            panelcontinue.Visible = false;
            panelreduce.Visible = false;
            panelreissue.Visible = true;
        }
        else
        {
            paneladd.Visible = false;
            panelcontinue.Visible = false;
            panelreduce.Visible = false;
            panelreissue.Visible = false;
        }

    }
    protected void BindDate()
    {
        ddlmonth1.Items.Clear();
        ddlmonth2.Items.Clear();
        ddlmonth3.Items.Clear();
        ddlyear1.Items.Clear();
        ddlyear2.Items.Clear();
        ddlyear3.Items.Clear();
        ddlbfyear.Items.Clear();
        ddlbfmonth.Items.Clear();
        int year = Convert.ToInt32(DateTime.Now.Year.ToString());
        for (int i = year-1; i < year + 3; i++)
        {
            ddlyear1.Items.Add(i.ToString());
            ddlyear2.Items.Add(i.ToString());
            ddlyear3.Items.Add(i.ToString());
            ddlbfyear.Items.Add(i.ToString());
        }
        for (int i = 1; i < 13; i++)
        {
            ddlmonth1.Items.Add(i.ToString());
            ddlmonth2.Items.Add(i.ToString());
            ddlmonth3.Items.Add(i.ToString());
            ddlbfmonth.Items.Add(i.ToString());
        }
        ddlbfmonth.SelectedValue = DateTime.Now.Month.ToString();
        ddlbfyear.SelectedValue = DateTime.Now.Year.ToString();
    }
    protected void btnadd_Click(object sender, EventArgs e)
    {
        string longid = Request.QueryString["longid"].ToString();
        string noid = Request.QueryString["noid"].ToString();
        if (ckbred1.Checked == true)
        {
            ReduceRecord reduce=new ReduceRecord();
            bool succ = reduce.ChangeReduceOprate(longid,noid,txbname.Text,txbaddress.Text,txbmonthmoney.Text,txbcheckarea.Text,
                                                    txbreducemoney.Text,ViewState["redoldreduce"].ToString(),ViewState["redoldstoptime"].ToString(),txbremark1red.Text,"");
            if (succ)
                WebHelper.AlertAndClose("增加成功，请继续其他操作");
            else
                WebHelper.Alert("增加失败");

        }
        else if (ckbkind1.Checked == true)
        {
            KindRecord kind = new KindRecord();
            bool succ = kind.ChangeKindOprate(longid, noid, txbkindaddress.Text, txbkindarea.Text, txbkindusearea.Text, 
                                ViewState["kindoldusearea"].ToString(), txbremark1kind.Text, ViewState["kindstoptime"].ToString(), "");
            if (succ)
                WebHelper.AlertAndClose("增加成功，请继续其他操作");
            else
                WebHelper.Alert("增加失败");
        }
        else
            WebHelper.AlertAndRefresh("未选择调整的项");
      
    }
    protected void btnreduce_Click(object sender, EventArgs e)
    {

    }
    protected void btncontinue_Click(object sender, EventArgs e)
    {
        string longid=Request.QueryString["longid"].ToString();
        string noid=Request.QueryString["noid"].ToString();
        ReduceRecord reduce = new ReduceRecord();
        SubsidyRecord subsidy = new SubsidyRecord();
        KindRecord kind = new KindRecord();
        int subtime = Convert.ToInt32(ddlyear1.SelectedValue) * 100 + Convert.ToInt32(ddlmonth1.SelectedValue);
        int redtime = Convert.ToInt32(ddlyear2.SelectedValue) * 100 + Convert.ToInt32(ddlmonth2.SelectedValue);
        int kindtime = Convert.ToInt32(ddlyear3.SelectedValue) * 100 + Convert.ToInt32(ddlmonth3.SelectedValue);
        bool succred = false, succsub = false, succkind = false;
        if (ckbreduce.Checked == true)
            succred = reduce.ContinueReduceOprate(longid, noid, redtime.ToString(), txbremarkred.Text, "");
        if (ckbsubsidy.Checked == true)
            succsub = subsidy.ContinueSubsidyOprate(longid, noid, subtime.ToString(), txbremarksub.Text,"");
        if (ckbkind.Checked == true)
            succkind = kind.ContinueKindOprate(longid, noid, kindtime.ToString(), txbremarkkind.Text, "");
        if (succred || succsub || succkind)
            WebHelper.AlertAndClose("续办成功，请继续其他操作");
        else
            WebHelper.Alert("续办失败");
    }
    protected void btnreissue_Click(object sender, EventArgs e)
    {
        Apply apply = new Apply();
        int time=Convert.ToInt32(ddlbfyear.SelectedValue)*100 + Convert.ToInt32(ddlbfmonth.SelectedValue);
        //Response.Write(time);
        if (apply.ApplyReissue(Request.QueryString["longid"].ToString(),txbbfmoney.Text, time, txbbfremark.Text))
            WebHelper.AlertAndClose("申请补发成功！");
        else
            WebHelper.Alert("本月已申请补发或申请补发失败！");
    }
}
